github.com/jasonpaulos/decipher-22-pyteal-talk
go-algorand repo: Box OpCodes, AVM-8 paramssubmit choice Burj Khalifa - Dubai¶Taipei += 1 ✅¶Shanghai += 1 ✅¶Merdeka += 26 ❌¶App.box_get() and App.box_put()¶num_choices = Int(7)
choice = Bytes(b"\x01") # "Burj Khalifa - Dubai"'s index as bytes
sender_box = App.box_get(Txn.sender())
key_prefix = Bytes(b"option_count_")
choice_key = Concat(key_prefix, choice)
old_key = ScratchVar(TealType.bytes)
submit_expr = Seq(
Assert(Btoi(choice) < num_choices),
If(sender_box.hasValue()).Then(
# clear sender's previous response
old_key.store(Concat(key_prefix, sender_box.value())),
App.globalPut(
old_key.load(),
App.globalGet(old_key.load()) - Int(1),
),
),
App.box_put(Txn.sender(), choice),
App.globalPut(choice_key, App.globalGet(choice_key) + Int(1)),
)
App.box_*() APIApp.box_create(name, size)- boxes of size $\leq$ 4KB¶App.box_length(name) - opcode box_len¶App.box_delete(name) - opcode box_del¶App.box_replace(name, idx, L) - set part of box¶App.box_extract(name, idx, L) - get part of box¶App.box_put(name, value) - set contents (may create)¶App.box_get(name) - get everything (fail if size > 4KB)¶Property |
Global Storage |
Local Storage |
Box Storage |
|---|---|---|---|
| Max(|key| + |value|) | 128 bytes | 128 bytes | 32,832 bytes |
| Max storage | 64 pairs or 8 KB | 16 pairs or 2 KB | $\infty$ |
| On-Chain Visibility | Public | Public | Private: boxes only visible to their app |
| Best Case MBR* | 0.40 Algo/KB | 0.40 Algo/KB | 0.41 Algo/KB |
| Account for MBR | Creator Account | Opt-In Account | App Account |
| When App is Deleted | Automatically erased | same as Global | Forever lost on chain** |
| External Ref in Txn | None | None | Box reference required |
methods - allow interacting with Poll App¶delete for reasons to be explained shortly)¶OnComplete Actions for App Transactions¶| Value | Name | Description |
|---|---|---|
| 0 | NoOp | Execute ApprovalProgram only |
| 1 | OptIn | Allocate local state and execute ApprovalProgram |
| 2 | CloseOut | Execute ApprovalProgram and clear local state |
| 3 | ClearState | Execute ClearStateProgram and clear locals (even if rejects) |
| 4 | UpdateApplication | Execute ApprovalProgram and update programs |
| 5 | DeleteApplication | Execute ApprovalProgram and delete the app |
Router and its M.O.E. Questions¶
Router and its M.O.E. Questions¶Router constructs the Teal code necessary to delegate application transactions to either a bare app call action or a method based on answers to:¶bare app call? If not, which method selected?¶OnComplete is requested?¶exists? (Conversely, being created?)¶Router Initialization¶
# WARNING: STUBS ARE FOR ROUTER-ILLUSTRATION PURPOSES ONLY!!!
del_action = OnCompleteAction.call_only(Seq())
router = Router(
name="OpenPollingApp",
descr="This is a polling application.",
bare_calls=BareCallActions(delete_application=del_action),
)
approval, clear, json_contract = router.compile_program(version=8)
JSON Contract (with no methods)¶print(json.dumps(json_contract.dictify(), indent=2))
approval, clear, json_contract = router.compile_program(version=8)
print(approval)
delete¶show() # custom method showing relevant Teal
methods to route with @router.method¶open() and close()¶@router.method(name="open")
def open_poll() -> Expr:
"""Marks this poll as open."""
return Seq()
@router.method(name="close")
def close_poll() -> Expr:
"""Marks this poll as closed."""
return Seq()
approval, clear, json_contract = router.compile_program(version=8)
print(json.dumps(json_contract.dictify(), indent=2))
PyTeal Type |
ARC-4 Type |
Dynamic / Static |
Description |
|---|---|---|---|
|
Static |
An 8-bit unsigned integer |
|
|
IFF |
A fixed-length array with N elements |
|
|
Dynamic |
Variable-length byte array |
@router.method
def submit(choice: abi.Uint8) -> Expr:
"""Submit a response to the poll.
Args:
choice: The choice made by the sender.
"""
return Seq()
opts = OptimizeOptions(scratch_slots=True)
approval, clear, json_contract = router.compile_program(version=8, optimize=opts)
# -----------------------------------------------------------------^^^^^^^^^^^^^
submit(choice) portion of JSON contract¶show() # lines 20-33 of the JSON contract
submit(choice)¶show()
contract/contract.py¶show()
contract/contract.json¶show()
contract/approval.teal¶show()